Libraries for image processing
In [1]:
from PIL import Image
In [2]:
mac = Image.open("example.jpg")
In [3]:
mac
Out[3]:
- getting size
In [4]:
mac.size
Out[4]:
(1993, 1257)
In [5]:
mac.format
Out[5]:
'JPEG'
In [6]:
mac.format_description
Out[6]:
'JPEG (ISO 10918)'
Cropping an image¶
crop function takes 4 arguments left top right and bottom
- left distance from left of the image to the given argument
- right distance from left of the image to the given argument
- top distance from top of the image to the given argument
- bottom distance from top of the image to the given argument
In [7]:
left = 880
top = 850
right = 1150
bottom = 1250
mac.crop((left, top, right, bottom))
Out[7]:
copy and pasting¶
In [8]:
computer = mac.crop((left, top, right, bottom))
mac.paste(im=computer,box=(0,0))
mac
Out[8]:
Resizing¶
In [9]:
mac.size
Out[9]:
(1993, 1257)
In [10]:
mac.resize((800,500))
Out[10]:
Rotate images¶
In [11]:
mac.rotate(90)
Out[11]:
In [12]:
mac.close()
managing transparency¶
In [13]:
blue = Image.open("blue_color.jpg")
red = Image.open("red_color.jpg")
In [14]:
red.convert("RGB")
blue.convert("RGB")
Out[14]:
In [15]:
red
Out[15]:
In [16]:
blue
Out[16]:
In [17]:
red.putalpha(128)
blue.putalpha(128)
In [18]:
red.show()
In [19]:
blue.show()
Combining images¶
In [20]:
red.paste(im=blue,box=(0,0),mask=blue)
In [21]:
red.show()
In [22]:
red.save("purple.png")
In [23]:
red.close()
blue.close()
practice task to get first 3 pencils¶
In [24]:
pencil = Image.open("./pencils.jpg")
pencil
Out[24]:
In [25]:
height, width = pencil.size
top = 0
left = 0
bottom = height * .076
right = width * .44
pencil.crop((left,top,right,bottom))
Out[25]: